package com.lynx.listener;

import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.ProgressListener;
import org.springframework.stereotype.Component;

import com.lynx.controller.model.Progress;

/**
 * @author chris.liu
 * @version 5/8/14 4:44 PM
 */
@Component
public class FileUploadProgressListener implements ProgressListener {
	private HttpSession session;

    public FileUploadProgressListener() {

    }

    public FileUploadProgressListener(HttpSession session) {
		this.session = session;
		Progress status = new Progress();
		session.setAttribute("progress", status);
	}

	public void setSession(HttpSession session) {
		this.session = session;
		Progress progress = new Progress();
		session.setAttribute("progress", progress);
	}

	@Override
	public void update(long pBytesRead, long pContentLength, int pItems) {
		Progress progress = (Progress) session.getAttribute("progress");
		progress.setBytesRead(pBytesRead);
		progress.setContentLength(pContentLength);
		progress.setItems(pItems);
		session.setAttribute("progress", progress);
	}
}
